'use strict';

module.exports = function(sequelize, DataTypes) {
    const ledger = sequelize.define('ledger', {
        id: {
            type: DataTypes.BIGINT(20).UNSIGNED,
            allowNull: false,
            autoIncrement: true,
            primaryKey: true,
            readonly: true
        },
        status: {
            type: DataTypes.ENUM,
            values: ['pending', 'matched', 'not_matched', 'ignore', 'waiting'], //未盘, 盘准, 盘亏, 不盘, 待确认
            defaultValue: 'pending'
        },
        organization_id: {
            type: DataTypes.BIGINT(20).UNSIGNED,
            allowNull: false,
        },
        key: {
            type: DataTypes.STRING(191),
            allowNull: false,
        }, //文件标识
        //tag: {
        //    type: DataTypes.STRING
        //}, //标签 'id,id'
        spu_id: {
            type: DataTypes.BIGINT(20).UNSIGNED
        },
        type: {
            type: DataTypes.STRING
        }, //设备类型
        generic: {
            type: DataTypes.STRING
        }, //通用名
        name: {
            type: DataTypes.STRING
        }, //仪器名称
        brand: {
            type: DataTypes.STRING
        }, //品牌
        asset_number: {
            type: DataTypes.STRING
        }, //资产编号
        model_number: {
            type: DataTypes.STRING
        }, //型号
        serial_number: {
            type: DataTypes.STRING
        }, //序列号
        department: {
            type: DataTypes.STRING
        }, //科室
        localtion: {
            type: DataTypes.STRING
        }, //安装地点
        installDate: {
            type: DataTypes.DATE
        }, //安装日期
        purchased_date: {
            type: DataTypes.DATE
        }, //购置日期
        amount: {
            type: DataTypes.INTEGER
        }, //数量
        price: {
            type: DataTypes.FLOAT
        }, //金额
        manufacturer: {
            type: DataTypes.STRING
        }, //生产厂商
        supplier: {
            type: DataTypes.STRING
        }, //供应商
        start_date: {
            type: DataTypes.DATE
        }, //启用日期
        accept_date: {
            type: DataTypes.DATE
        }, //验收日期
        years: {
            type: DataTypes.INTEGER
        }, //折旧年限
        salvage: {
            type: DataTypes.FLOAT
        }, //残值率
        memo: {
            type: DataTypes.STRING
        }, //备注
        user_defined: {
            type: DataTypes.TEXT
        }, //自定义字段
        error: {
            type: DataTypes.STRING
        },
        md5: {
            type: DataTypes.STRING
        },
        derpartment_confirmed:{
            type: DataTypes.BOOLEAN,
            defaultValue: false
        },
        created_by: {
            type: DataTypes.BIGINT(20).UNSIGNED,
            allowNull: false,
            readonly: true
        },
        updated_by: {
            type: DataTypes.BIGINT(20).UNSIGNED,
        }
    }, {
        tableName: 'sophic_ledgers',

        underscored: true,

        indexes: [{
            fields: ['key']
        }, {
            fields: ['organization_id']
        }],
        //defaultScope: {
        //    attributes: ['id', 'ledger_id', 'tag_id']
        //},
        classMethods: {

            operationLog: true,

            associate: function(models) {
                ledger.hasMany(models.LedgerTag, {
                    constraints: false,
                })
            }
        }
    });

    return ledger;
};